package cn.com.yang.datastruct.stack;

import java.util.Stack;

/**
 * 利用栈校验
 * @since 2023.07.18
 */
public class StackTest2 {
    public static void main(String[] args) {
        String s = "12<a[b{c}]>";
        char[] sa = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        char ch;
        boolean flag =false;
        for (char c:sa){
            if (c=='<'||c=='['||c=='{'){
                stack.push(c);
            }
            if (c=='>'|| c==']' || c=='}'){
                ch= stack.pop();
                if (ch=='<' && c=='>' || (ch=='['&& c==']')||(ch=='{'&&c=='}')){
                    flag=true;
                }else {
                    flag=false;
                    System.out.println("符号："+ch +"和符号："+c+"不匹配");
                }
            }
        }
        if (stack.isEmpty() && flag){
            System.out.println("所有匹配是正确的");
        }else {
            System.out.println("特殊符号匹配错误");
        }
    }
}
